$('#selectDept').on('hidden.bs.modal', function () {
    //$("#deptCode").val('');
    $("#columnDeptName").val('');
    newCheck.setUnCheckedByName('columnDept');
});

$(function(){
    regValidate(["#sicknessForm"]);
    newCheck.initByName('columnDept');
});


function onClickTab(obj) {
    var id = $(obj).attr('id');
    if (id == '_tab1') {
        $('#_tab1').addClass('active');
        $('#_tab2').removeClass('active');
        $('#_tab3').removeClass('active');
        $('#_div2').css('display', 'none');
        $('#_div3').css('display', 'none');
        $('#_div1').css('display', 'block');
    } else if (id == '_tab2') {
        $('#_tab2').addClass('active');
        $('#_tab1').removeClass('active');
        $('#_tab3').removeClass('active');
        $('#_div1').css('display', 'none');
        $('#_div3').css('display', 'none');
        $('#_div2').css('display', 'block');
    } else {
        $('#_tab3').addClass('active');
        $('#_tab1').removeClass('active');
        $('#_tab2').removeClass('active');
        $('#_div1').css('display', 'none');
        $('#_div2').css('display', 'none');
        $('#_div3').css('display', 'block');
    }
}


function doSaveSick(){
    if(!$("#sicknessForm").valid()) return;//校验不通过
    var id = $("#id").val();
    var deptCodes = getLabelNoteStr('dept', false);
    var summarize = $("#summarize").val();
    var clinical = $("#clinical").val();
    var sickDesc = $.trim(ue.getContent());
    var sicknessName = $("#sicknessName").val();
    var data = getFromData('#sicknessForm');
    if(deptCodes == null || deptCodes == ''){
        toastr.error('至少添加一个二级科室');
        return;
    }
    var sortNum = (!data.sortNum || data.sortNum == '') ? 9999 : data.sortNum;
    var postData = {
        id : id,
        sicknessCode : data.sicknessCode,
        idctenCode : data.idctenCode,
        sicknessName : sicknessName,
        shortName : data.shortName,
        otherInfoUrl : data.otherInfoUrl,
        source : data.source,
        sortNum : sortNum,
        alias : data.alias,
        symptom :data.symptom,
        deptCodes : deptCodes == 'all' ? '' : deptCodes,
        clinical : clinical,
        summarize : summarize,
        sickDesc : sickDesc,
    };
    var url;
    if(id == null || id == ''){
        url = basePath + 'basicSicknessInfo/addSickness?t=' + new Date().getTime();
    }else{
        url = basePath + 'basicSicknessInfo/editSickness?t=' + new Date().getTime();
    }
    $.ajax({
        type: 'post',
        url: url,
        dataType: 'json',
        contentType : "application/json; charset=UTF-8",
        data: JSON.stringify(postData),
        beforeSend:function () {
            showLoading();
        }, complete:function(){
            hideLoading();
        }, success : function (data) {
            if(data.result){
                toastr.success(data.message);
                doBack();
            }else{
                toastr.error(data.message);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}


function selectDept(){
    $("#columnDeptName").val('');
    doDeptRefresh();
    var columnDeptCodes = getLabelNoteStr('dept', false);
    if (columnDeptCodes != 'all') {
        var columnDeptList = columnDeptCodes.split(',');
        newCheck.setUnCheckedByName('columnDept');
        $.each(columnDeptList,function(n,value) {
            newCheck.setCheckedByNameAndValue('columnDept', value);
        });
    }
    $("#selectDept").modal(true);
}


function doAddColumnDept() {
    var $checked=$('input[type=checkbox][name="columnDept"]:checked');
    var tagList = [];
    if (!$checked || $checked.length == 0) {
        toastr.error('请选择指定科室');
        return;
    }
    $.each($checked,function (index,val) {
        tagList.push({
            value : $(this).val(),
            text : $(this.parentElement).parent().attr('idd'),
        });
    });
    $('#selectDept').modal('hide');
    var columnDeptCodes = getLabelNoteStr('dept', false);
    if (columnDeptCodes == 'all') {
        htmlLableByList(tagList, "dept");
    } else {
        addLableByList(tagList, "dept");
    }
}

function doDeptRefresh(){
        var name = $.trim($("#columnDeptName").val());
        if(!name){
            $("#depts label").show();
            return false;
        }
        //全部隐藏
        $("#depts label").hide();
        $("label[idd*="+ name +"]").show();
    }

    function doCheckDataChange() {
        var deptCodes = getLabelNoteStr('dept', false);
        var data = getFromData('#sicknessForm');
            if (!Compare(data,dto) || CompareStr(deptCodes,dto.deptInfo)) {
                swalClosePageToastr(doBack);
                return;
            }
        doBack();
    }

    function CompareStr(str,list){
        var listStr = '';
        if(list == null){
            listStr = '';
        }else{
        for (var i = 0; i <= list.length - 1; i++) {
            if (i < list.length - 1) {
                if (list[i] != '') {
                    listStr += list[i].deptCode + ',';
                }
            } else {
                if (list[i] != '') {
                    listStr += list[i].deptCode;
                }
            }
        }
        }
        if (str != listStr) {
            return true;
        }
       return false;
    }

    function doBack() {
        window.location.href = basePath + 'basicSicknessInfo/sicknessPage?t=' + new Date().getTime()+
            '&sicknessName='+searchInfo.sicknessName+'&oneLevelDeptCode='+searchInfo.oneLevelDeptCode+'&twoLevelDeptCode='+searchInfo.twoLevelDeptCode+ '&pageNo=' + searchInfo.pageNo + '&pageSize=' + searchInfo.pageSize;
   }


//************************************************************************************************************************

//初始化编辑器
window.UEDITOR_CONFIG = {
    // 为编辑器实例添加一个路径，这个不能被注释
    UEDITOR_HOME_URL: basePath+"js/ueditor/",
    // 服务器统一请求接口路径
    serverUrl:basePath+ "ueditor/dispatch",
    placeholder:"Please enter some text..."
};
//初始化编辑器
var ue = UE.getEditor('sickDesc', {
    initialFrameHeight:800,//编辑区域高度
    maximumWords:30000, autoHeightEnabled: false,
    autoFloatEnabled: true,allowDivTransToP: false,
    allowPreviewEmoticons : false,
    toolbars: [[
        'preview', //预览
        'source', //源代码
        'bold', //加粗
        'indent', //首行缩进
        'italic', //斜体meta
        'underline', //下划线
        'strikethrough', //删除线
        'subscript', //下标
        'fontborder', //字符边框
        'superscript', //上标
        'formatmatch', //格式刷
        'blockquote', //引用
        'pasteplain', //纯文本粘贴模式
        'horizontal', //分隔线
        'removeformat', //清除格式
        'time', //时间
        'date', //日期
        'link',
        'unlink', //取消链接
        'inserttitle', //插入标题
        // 'insertcode', //代码语言
        'fontfamily', //字体
        'fontsize', //字号
        // 'paragraph', //段落格式
        // 'emotion', //表情
        'spechars', //特殊字符
        'simpleupload', //单图上传
        'justifyleft', //居左对齐
        'justifyright', //居右对齐
        'justifycenter', //居中对齐
        'justifyjustify', //两端对齐
        'forecolor', //字体颜色
        'backcolor', //背景色
        // 'insertorderedlist', //有序列表
        // 'insertunorderedlist', //无序列表
        // 'directionalityltr', //从左向右输入
        // 'directionalityrtl', //从右向左输入
        'rowspacingtop', //段前距
        'rowspacingbottom', //段后距
        'imagenone', //默认
        'imageleft', //左浮动
        'imageright', //右浮动
        'imagecenter', //居中
        'lineheight', //行间距
        'edittip ', //编辑提示
        'customstyle', //自定义标题
        'autotypeset', //自动排版
        // 'help', //帮助
        'cleardoc' //清空文档
    ]]
});

$(function () {
    if (dto && (dto.sickDesc != null || dto.sickDesc != '')) {
        ue.ready(function() {
            ue.body.innerHTML = dto.sickDesc ? dto.sickDesc : '';
            //判断是否详情
        });
    }
});


function showQrCode() {
    $("#qrcode").html("");
    var sickDesc = $.trim(ue.getContent());
    var postData = {
        sickDesc : sickDesc,
    };
    $.ajax({
        type: 'post',
        url: basePath + 'basicSicknessInfo/getQrCode',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data.result){
                var content = data.content;
                initQrCode(content.sickDesc);
                $("#scanQrCodePage").modal('show');
            }else{
                toastr.error(data.message);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}

function initQrCode(url) {
    $('#qrcode').qrcode({
        height: 240,
        width: 240,
        text: url
    });
}